package io.babystar.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.BasePayTypeEntity;
import io.babystar.modules.business.service.BasePayTypeService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 缴费方式表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-06-18 14:59:49
 */
@RestController
@RequestMapping("business/basepaytype")
public class BasePayTypeController {
    @Autowired
    private BasePayTypeService basePayTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:basepaytype:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        params.put("businessId",userEntity.getBusinessId().toString());
        PageUtils page = basePayTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:basepaytype:info")
    public R info(@PathVariable("id") Integer id){
		BasePayTypeEntity basePayType = basePayTypeService.getById(id);

        return R.ok().put("basePayType", basePayType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:basepaytype:save")
    public R save(@RequestBody BasePayTypeEntity basePayType){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        basePayType.setBusinessId(userEntity.getBusinessId().intValue());
        basePayType.setCreator(userEntity.getUserId().toString());
        basePayType.setCreateTime(new Date());
		basePayTypeService.save(basePayType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:basepaytype:update")
    public R update(@RequestBody BasePayTypeEntity basePayType){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        basePayType.setBusinessId(userEntity.getBusinessId().intValue());
        basePayType.setUpdator(userEntity.getUserId().toString());
        basePayType.setUpdateTime(new Date());
		basePayTypeService.updateById(basePayType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:basepaytype:delete")
    public R delete(@RequestBody Integer[] ids){
		basePayTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据机构查询
     */
    @RequestMapping("/queryList")
    public R queryList(){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        List<BasePayTypeEntity> list = basePayTypeService.list(new QueryWrapper<BasePayTypeEntity>().eq("business_id", userEntity.getBusinessId()));

        return R.ok().put("list", list);
    }

}
